#include "YDWEBase.j"
#include "YDWEJumpTimer.j"
#include "YDWETriggerEvent.j"
#include "YDWETimerSystem.j"
#include "japi\\YDWEJapiEffect.j"
#include "japi\\YDWEAbilityState.j"
#include "AntiBJLeak\\detail\\UnitHasItemOfTypeBJ.j"
#include "japi\\YDWEJapiScript.j"

#include "_sys\\SystemInit.j"
#include "_init\\InitFunction.j"

// 游戏设置
#include "_game\\GameSetting.j"

#include "_ext_sys\\ExtFunctions.j"

#include "_util\\PropertyUtil.j"

// 模拟攻击弹道
#include "_util\\SimulateAttack.j"

#include "_init\\InitFilter.j"

// 死亡系统
#include "_sys\\UnitDie.j"

#include "_ui\\FdfFile.j"
#include "_ui\\UIInit.j"
#include "_util\\UnitUtil.j"
#include "_sys\\SelectUnit.j"
#include "_util\\MathUtil.j"
// 自定义控制台
#include "_ui\\UIConsole.j"

#include "_game\\player\\Hero.j"

// 伤害系统
// #include "_sys\\Damaged.j"

// 跳跃
#include "_game\\skill\\DSkill.j"

// 同步函数
#include "_ui\\SyncData.j"

// 中央计时器
#include "_sys\\CenterTimer.j"

// 技能
#include "_util\\SimulateAttac2.j"

#include "_util\\SimulateAttac3.j"

#include "_util\\SimulateAttac4.j"

#include "_util\\SimulateAttac5.j"

#include "_util\\SimulateAttac6.j"

// 新的伤害系统
#include "_sys\\SysDamage.j"

// 英雄攻击技能系统
#include "_sys\\AttackSys.j"

// 技能选择UI
#include "_ui\\ChooseSkillUI.j"

// buff管理
#include "_buff\\BuffVo.j"


// 随机技能系统
#include "_game\\skill\\RandomSkillSys.j"

// 字符串工具
#include "_util\\StringUtil.j"

// 游戏引导主文件
library main initializer init requires GameData, ChooseSkillUI

    globals
        group testG = CreateGroup()
        unit testU
        integer testI = 0
    endglobals

    function testRemove takes nothing returns nothing
        set testI = testI + 1
    endfunction

    // 玩家按下Esc异步调用
    function F5Action takes nothing returns nothing
        local integer uid = GetPlayerId(DzGetTriggerKeyPlayer())
        local integer i = 1
        local unit u = udg_player_select_hero[uid]
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local effect ect
        local group g = CreateGroup()
        local integer loopi = 0

        // call BJDebugMsg(" ================= 随机技能开始 ================")
        // call randomSkillId(uid)
        // loop
        //     exitwhen loopi > 2
        //     call BJDebugMsg("大技能ID = " + I2S(cardSkillId[uid * 8 + loopi]) + "  小技能ID = " + I2S(cardSkillIdSmall[uid * 8 + loopi]))
        //     set loopi = loopi + 1
        // endloop
        // call BJDebugMsg(" ================= 随机技能结束 ================")


        // call BJDebugMsg("随机1 = " + I2S(GetRandomInt(0, 0)))
        // call BJDebugMsg("随机2 = " + I2S(GetRandomInt(2, 2)))

        // call BJDebugMsg("飞剑术：飞剑距离增加4000")
        // set ct_flyBoom[uid] = true
        // set ct_flyDistance[uid] = ct_flyDistance[uid] + 4000.0
        // call BJDebugMsg("飞剑术：火焰剑")
        // set ct_flyFire[uid] = true
        // call BJDebugMsg("飞剑术：飞剑数量+1")
        // set ct_flyCount[uid] = ct_flyCount[uid] + 1
        // call BJDebugMsg("飞剑术：飞剑贯穿+2")
        // set ct_flyPzCount[uid] = ct_flyPzCount[uid] + 2
        // call BJDebugMsg("飞剑术：连射次数+1")
        // set ct_flyAttCount[uid] = ct_flyAttCount[uid] + 1

        // =========================== 特效范围测试 start ====================================
        // set ect = AddSpecialEffect("hs_shandian.mdx", x, y)
        // call EXSetEffectSize(ect, 2.0)
        // call EXSetEffectSpeed( ect, 2.00 )
        // call EXEffectMatRotateZ(ect, GetRandomReal(0, 30))
        // call YDWETimerDestroyEffect( 0.1, ect )
        // call DestroyEffect(ect)
        // call CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // =========================== 特效范围测试 end ====================================

        // ==================================== 单位组测试
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // set testU = u
        // call GroupAddUnit(testG, u)
        // set u = CreateUnit(Player(uid), 'hpea', x + 200.0, y, 270.0)
        // call GroupAddUnit(testG, u)
        // call GroupAddUnit(testG, u)
        // call GroupAddUnit(testG, u)
        // call BJDebugMsg("单位组的数量是：" + I2S(CountUnitsInGroup(testG)))

        // call ForGroup(testG, function testRemove)
        // // call GroupRemoveUnit(testG, u)
        // call RemoveUnit(u)
        // call BJDebugMsg("记录的数量是：" + I2S(testI))
        // call BJDebugMsg("单位组的数量是：" + I2S(CountUnitsInGroup(testG)))

        // ==================================== 单位组测试

        // call BJDebugMsg("F5 Action")

        // call SetUnitAnimation( udg_player_select_hero[uid], "Attack" )

        // call getNearestUnit(uid)

        // call releaseFanAttack2(uid, GetUnitX(udg_player_select_hero[uid]), GetUnitY(udg_player_select_hero[uid]), 5000, 0, 10, 7, 50, "tsw_feijian.mdx")

        call skillShowChooseIcoUI(uid, 0)

        // call BJDebugMsg("泄露字符串：" + I2S(GetCacheStringCount()))

        // call BJDebugMsg("双击计时器：" + R2S(TimerGetElapsed(double_timer)))


        // 随机不重复的数组，1-10种随机6个
        // call randomIntToHash(uid, 20241029, 1, 10, 6)

        // loop
        //     exitwhen i > 6
        //     call BJDebugMsg("随机数：" + I2S(getHashRandomInt(uid, 20241029, i)))
        //     set i = i + 1
        // endloop

        // // 排泄清空
        // call destroyRandomHash(uid, 20241029)

        call BJDebugMsg("==================================")

    endfunction

    // 输出11执行
    function Enter11Action takes nothing returns nothing
        local integer uid = GetPlayerId(GetTriggerPlayer())
        local integer i
        local integer count
        local real cxTime = GetRandomReal(0, 3)
        local real cdTime = GetRandomReal(2, 10)

        // 飞剑术测试
        set i = GetRandomInt(1, 28)


        call BJDebugMsg("随机技能Id：" + I2S(i) + " 施法持续时间：" + R2S(cxTime) + " 冷却时间：" + R2S(cdTime))
        call newSkillButton(uid, i, "ReplaceableTextures\\CommandButtons\\BTNFlare.blp", cxTime, cdTime)


        // if i == 1 then
        //     call BJDebugMsg("飞剑术：飞剑距离增加1000")
        //     set ct_flyDistance[uid] = ct_flyDistance[uid] + 1000.0
        // elseif i == 2 then
        //     call BJDebugMsg("飞剑术：火焰剑")
        //     set ct_flyFire[uid] = true
        // elseif i == 3 then
        //     call BJDebugMsg("飞剑术：飞剑数量+1")
        //     set ct_flyCount[uid] = ct_flyCount[uid] + 1
        // elseif i == 4 then
        //     call BJDebugMsg("飞剑术：飞剑贯穿+1")
        //     set ct_flyPzCount[uid] = ct_flyPzCount[uid] + 1
        // elseif i == 5 then
        //     call BJDebugMsg("飞剑术：飞剑命中单位后爆炸")
        //     set ct_flyBoom[uid] = true
        // elseif i == 6 then
        //     call BJDebugMsg("飞剑术：连射次数+1")
        //     set ct_flyAttCount[uid] = ct_flyAttCount[uid] + 1
        // elseif i == 7 then
        //     call BJDebugMsg("火焰弹：爆炸范围+20%")
        //     set ct_hydBoomRangZf[uid] = ct_hydBoomRangZf[uid] + 0.2
        // elseif i == 8 then
        //     set count = GetRandomInt(1, 3)
        //     call BJDebugMsg("火焰弹：火焰弹数量+" + I2S(count))
        //     set ct_hydAttCount[uid] = ct_hydAttCount[uid] + count
        // elseif i == 9 then
        //     call BJDebugMsg("掌心雷：爆炸范围+20%")
        //     set ct_zxlBoomRang[uid] = ct_zxlBoomRang[uid] + 0.2
        // elseif i == 10 then
        //     call BJDebugMsg("掌心雷：爆炸伤害+50%")
        //     set ct_zxlBoomRangZf[uid] = ct_zxlBoomRangZf[uid] + 0.5
        // elseif i == 11 then
        //     set count = GetRandomInt(1, 2)
        //     call BJDebugMsg("掌心雷：掌心雷数量+" + I2S(count))
        //     set ct_zxlAttCount[uid] = ct_zxlAttCount[uid] + count
        // elseif i == 12 then
        //     call BJDebugMsg("掌心雷：命中单位后会麻痹")
        //     set ct_zxlIsMB[uid] = true
        // elseif i == 13 then
        //     call BJDebugMsg("掌心雷：麻痹概率+20%")
        //     set ct_zxlIsMB[uid] = true
        //     set ct_zlxMbRate[uid] = ct_zlxMbRate[uid] + 0.2
        // elseif i == 14 then
        //     call BJDebugMsg("掌心雷：麻痹时间+0.5秒")
        //     set ct_zxlIsMB[uid] = true
        //     set ct_zlxMBLevel[uid] = ct_zlxMBLevel[uid] + 1
        // elseif i == 15 then
        //     call BJDebugMsg("闪电链：闪电单位+1")
        //     set ct_sdlMzCount[uid] = ct_sdlMzCount[uid] + 1
        // elseif i == 16 then
        //     call BJDebugMsg("闪电链：持续伤害次数+1")
        //     set ct_sdlIsChixu[uid] = true
        //     set ct_sdlCXCount[uid] = ct_sdlCXCount[uid] + 1
        // elseif i == 17 then
        //     call BJDebugMsg("寒冰箭：连续释放次数+1")
        //     set ct_hbjCount[uid] = ct_hbjCount[uid] + 1
        // elseif i == 18 then
        //     call BJDebugMsg("寒冰箭：伤害+80%")
        //     set ct_hbjDmZf[uid] = ct_hbjDmZf[uid] + 0.8
        // elseif i == 19 then
        //     call BJDebugMsg("寒冰箭：命中单位冻结时间+1秒")
        //     set ct_hbjDongj[uid] = true
        //     set ct_hbjDongjLevel[uid] = ct_hbjDongjLevel[uid] + 2
        // elseif i == 20 then
        //     call BJDebugMsg("寒冰箭：命中单位后造成减速")
        //     set ct_hbjIsJS[uid] = true
        // elseif i == 21 then
        //     call BJDebugMsg("寒冰箭：命中单位后造成减速+10%")
        //     set ct_hbjIsJS[uid] = true
        //     set ct_hbjJSLevel[uid] = ct_hbjJSLevel[uid] + 2
        // elseif i == 22 then
        //     call BJDebugMsg("寒冰箭：命中单位后冻伤持续伤害")
        //     set ct_hbjIsDS[uid] = true
        // elseif i == 23 then
        //     call BJDebugMsg("寒冰箭：命中单位后冻伤持续伤害次数+3")
        //     set ct_hbjIsDS[uid] = true
        //     set ct_hbjDSCount[uid] = ct_hbjDSCount[uid] + 3
        // elseif i == 24 then
        //     call BJDebugMsg("寒冰箭：贯穿+1")
        //     set ct_hbjPzCount[uid] = ct_hbjPzCount[uid] + 1
        // elseif i == 25 then
        //     call BJDebugMsg("寒冰箭：命中单位后分裂")
        //     set ct_hbjIsBoom[uid] = true
        // elseif i == 26 then
        //     call BJDebugMsg("寒冰箭：命中单位后分裂+1")
        //     set ct_hbjIsBoom[uid] = true
        //     set ct_hbjBoomCount[uid] = ct_hbjBoomCount[uid] + 1
        // elseif i == 27 then
        //     call BJDebugMsg("寒冰箭：命中分裂的小箭造成减速")
        //     set ct_hbjBoomJS[uid] = true
        // elseif i == 28 then
        //     call BJDebugMsg("英雄攻击力+30")
        //     call FF_AddAttack(udg_player_select_hero[uid], 30)
        // endif


    endfunction


    private function init takes nothing returns nothing
        local trigger tri = CreateTrigger()
        local integer uid = 0
        call BJDebugMsg("====================== 游戏触发加载成功 =====================")


        // 给在线的玩家创建一个临时英雄
        loop
            exitwhen uid > PLAYER_NUMBER
            if isPlayerOnline(uid) then
                set udg_player_select_hero[uid] = CreateUnit(Player(uid), 'N000', GetRectCenterX(getPlayerStartRect(uid)), GetRectCenterY(getPlayerStartRect(uid)), 0.0)
    
                // 设置攻击力
                call FF_SetAttack(udg_player_select_hero[uid], 200)
                // 生命值
                call FF_SetMaxHp(udg_player_select_hero[uid], 10000)
                call FF_SetHp(udg_player_select_hero[uid], 10000)
                // 护甲
                call FF_SetArmor(udg_player_select_hero[uid], 500)
                // 设置全属性
                // call FF_SetAllAttr(udg_player_select_hero[uid], 100000)

                // 设置类型
                call FF_SetUnitType(udg_player_select_hero[uid], 1)

                // 设置攻击间隔
                call subUnitAttackInterval(udg_player_select_hero[uid], 2.0)
                // 增加攻击速度
                // call SetUnitState( udg_player_select_hero[uid], ConvertUnitState(0x51), 3.00 )
            endif
            set uid = uid + 1
        endloop


        // F5
        call DzTriggerRegisterKeyEventTrg(tri, 0, DzI2K(116))
        call TriggerAddAction(tri, function F5Action)

        set tri = CreateTrigger()
        call TriggerRegisterPlayerChatEvent( tri, Player(0), "11", true )
        call TriggerAddAction(tri, function Enter11Action)
    endfunction

endlibrary




